extensions: Don't perform strict comparisons on floating point values
authorTéo Mazars <teo.mazars@ensimag.fr>
Sat, 3 Aug 2013 16:15:15 +0000 (18:15 +0200)
committerTéo Mazars <teo.mazars@ensimag.fr>
Sat, 3 Aug 2013 16:17:25 +0000 (18:17 +0200)
extensions/HSL.c
extensions/HSV.c

index 542647474b8db8b5e186cb8b834d6612af63bb08..827e0285847506e27d11411725366b1671c4f8e5 100644 (file)
@@ -25,6 +25,7 @@
 
 #define MIN(a,b) ((a > b) ? b : a)
 #define MAX(a,b) ((a < b) ? b : a)
+#define EPSILON  1.0e-10
 
 static long  rgba_to_hsla     (char   *src,
                                char   *dst,
@@ -121,16 +122,16 @@ rgb_to_hsl_step (double* src,
   max = MAX (red, MAX (green, blue));
   min = MIN (red, MIN (green, blue));
 
-  if (max == red)
+  if (max - red < EPSILON)
     cpn_max = 0;
-  else if (max == green)
+  else if (max - green < EPSILON)
     cpn_max = 1;
   else
     cpn_max = 2;
 
   lightness = (max + min) / 2.0;
 
-  if (max == min)
+  if (max - min < EPSILON)
     {
       hue = saturation = 0;
     }
index a4ddd81a846e646ca769605edf28beab56903b67..c830a0c36ad6895d8bc9aad50ddef9268d18aa5a 100644 (file)
@@ -30,6 +30,7 @@
 
 #define MIN(a,b) (a > b) ? b : a;
 #define MAX(a,b) (a < b) ? b : a;
+#define EPSILON  1.0e-10
 
 static long rgba_to_hsva     (char *src,
                               char *dst,
@@ -183,25 +184,25 @@ rgba_to_hsv_step (char *src,
 
   chroma = value - min;
 
-  if (value == 0.0)
+  if (value < EPSILON)
     saturation = 0.0;
   else
     saturation = chroma / value;
 
-  if (saturation == 0.0)
+  if (saturation < EPSILON)
     {
       hue = 0.0;
     }
   else
     {
-      if (red == value)
+      if (fabs (red - value) < EPSILON)
         {
           hue = (green - blue) / chroma;
 
           if (hue < 0.0)
             hue += 6.0;
         }
-      else if (green == value)
+      else if (fabs (green - value) < EPSILON)
         hue = 2.0 + (blue - red) / chroma;
       else
         hue = 4.0 + (red - green) / chroma;